from django.contrib import admin

from shopper.models import CartInfos
# Register your models here.
from .models import *
# admin.site.register(Types)
# admin.site.register(CommodityInfos)
#修改title和header
admin.site.site_title = "母婴后台系统"
admin.site.site_header = "母婴电商后台管理系统"
admin.site.index_title = "母婴平台管理"
@admin.register(Types)
class TypesAdmin(admin.ModelAdmin):
    list_display = [x for x in list(
        Types._meta._forward_fields_map.keys())]
    list_filter = ['firsts']

@admin.register(CommodityInfos)
class CommodityInfosAdmin(admin.ModelAdmin):
    # list_display = [x for x in list(
    #     CommodityInfos._meta._forward_fields_map.keys())]
    # search_fields = ['name']
    # date_hierarchy = 'create'
    # def formfield_for_dbfield(self, db_field, request, **kwargs):
    #     if db_field.name == 'types':
    #         db_field.choices = [
    #             (x['seconds'],x['seconds']) for x in Types.objects.values('seconds')]
    #     return super().formfield_for_dbfield(db_field, request, **kwargs)

    #页面布局设置
    #通过fieldsets划分不同的字段分组，让新增和修改页的表单布局更清晰
    fieldsets = (
        #第一组
        ('商品信息',{
            'fields': ('name','sezes','types','price','discount')
        }),
        #第二组
        ('收藏数量',{
            #collapse,让这个分组默认为折叠（可展开）
            'classes': ('collapse',),
            'fields': ('likes',)
        }),
    )
    #将模型中的types字段下拉选择框水平的单选按钮
    #radio_fields = {'types': (admin.HORIZONTAL)}
    #admin.VERTICAL 垂直
    #数据排序，按照id升序（降序是-id)
    ordering = ('id',)
    #支持哪些字段点击表头进行访问
    sortable_by = ('price','discount')
    #定义列表页显示的模型字段
    list_display = ['id','name','sezes','types','price','discount']
    list_filter = ['types']
    #设置分页每页显示的数据条数
    list_per_page = 100
    #最大显示数量
    list_max_show_all = 200
    #允许在数据列表页面直接编辑name字段
    list_editable = ['name']
    # 在列表页面添加搜索框，支持按照name和type字段的值进行搜索数据
    search_fields = ['name','types']
    # 启用另存为按钮
    save_on_top = True
    #下拉框位置，False表示不显示在顶部
    actions_on_top = False
    actions_on_bottom = True